
package com.dao;

import com.entity.Question;
import com.utils.PropertiesUtils;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class QuestionDao {
    public QuestionDao() {
    }

    public void add(Question q) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "insert into question(e_id,q_fpath) values(?,?)";
        Object[] params = new Object[]{q.getE_id(), q.getQ_fpath()};
        queryRunner.update(sql, params);
    }

    public void delete(Integer id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "delete from question where q_id = ?";
        queryRunner.update(sql, id);
    }

    public void update(Question q) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "update question set e_id=?,q_fpath=? where q_id=?";
        Object[] params = new Object[]{q.getE_id(), q.getQ_fpath(), q.getQ_id()};
        queryRunner.update(sql, params);
    }

    public List<Question> list(Question question) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from question";
        List<Question> list = (List)queryRunner.query(sql, new BeanListHandler(Question.class));
        return list;
    }

    public Question findById(Integer id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(PropertiesUtils.getDataSource());
        String sql = "select * from question where q_id = ?";
        Question question = (Question)queryRunner.query(sql, new BeanHandler(Question.class), new Object[]{id});
        return question;
    }
}